package com.pkk.cloud.support.gataway.spring.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @description: 自定义全局过滤器
 * @author: peikunkun
 * @create: 2019-08-23 11:22
 **/
@Slf4j
public class CustomGlobalFilter implements Ordered, GlobalFilter {

  /**
   * 过滤规则
   *
   * @param exchange
   * @param chain
   * @return
   */
  @Override
  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    final String host = exchange.getRequest().getURI().getHost();
    final String path = exchange.getRequest().getURI().getPath();
    log.info("visit path :" + path + " from " + host);
    return chain.filter(exchange);
  }

  /**
   * 过滤器顺序级别最高级别
   *
   * @return
   */
  @Override
  public int getOrder() {
    return Ordered.LOWEST_PRECEDENCE;
  }
}
